package dj.service.impl;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dj.Constant;
import dj.dao.BaseDao;
import dj.model.FriendGroup;
import dj.model.Menu;
import dj.model.MenuAuth;
import dj.model.SysRoleMembers;
import dj.model.SysRoles;
import dj.model.Tuser;
import dj.pageModel.Puser;
import dj.service.FriendService;
import dj.service.RepairService;
import dj.service.UserService;

@Service("repairService")
public class RepairServiceImpl implements RepairService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(RepairServiceImpl.class);

	private String[] baseGroup = { "我的好友", "陌生人", "黑名单" };

	private BaseDao<FriendGroup> friendGroupDao;

	private BaseDao<Menu> menuDao;

	private BaseDao<MenuAuth> menuAuthDao;

	private BaseDao<Tuser> userDao;

	private BaseDao<SysRoles> sysRoleDao;

	private BaseDao<SysRoleMembers> sysRoleMemberDao;

	private UserService userService;

	private FriendService friendService;

	public BaseDao<FriendGroup> getFriendGroupDao() {
		return friendGroupDao;
	}

	@Autowired
	public void setFriendGroupDao(BaseDao<FriendGroup> friendGroupDao) {
		this.friendGroupDao = friendGroupDao;
	}

	public BaseDao<Menu> getMenuDao() {
		return menuDao;
	}

	@Autowired
	public void setMenuDao(BaseDao<Menu> menuDao) {
		this.menuDao = menuDao;
	}

	public BaseDao<MenuAuth> getMenuAuthDao() {
		return menuAuthDao;
	}

	@Autowired
	public void setMenuAuthDao(BaseDao<MenuAuth> menuAuthDao) {
		this.menuAuthDao = menuAuthDao;
	}

	public BaseDao<Tuser> getUserDao() {
		return userDao;
	}

	@Autowired
	public void setUserDao(BaseDao<Tuser> userDao) {
		this.userDao = userDao;
	}

	public BaseDao<SysRoles> getSysRoleDao() {
		return sysRoleDao;
	}

	@Autowired
	public void setSysRoleDao(BaseDao<SysRoles> sysRoleDao) {
		this.sysRoleDao = sysRoleDao;
	}

	public BaseDao<SysRoleMembers> getSysRoleMemberDao() {
		return sysRoleMemberDao;
	}

	@Autowired
	public void setSysRoleMemberDao(BaseDao<SysRoleMembers> sysRoleMemberDao) {
		this.sysRoleMemberDao = sysRoleMemberDao;
	}

	public FriendService getFriendService() {
		return friendService;
	}

	@Autowired
	public void setFriendService(FriendService friendService) {
		this.friendService = friendService;
	}

	public UserService getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 启动时检测基本组是否存在，不存在则创建
	 */
	public void repairFriendGroup() {
		logger.info("初始化基本好友组...");
		for (int i = 0; i < baseGroup.length; i++) {
			FriendGroup fg = friendGroupDao.get("from FriendGroup where id = '" + i + "'");
			if (null == fg) {
				fg = new FriendGroup();
				fg.setId(i + "");
				fg.setGroupname(baseGroup[i]);
				fg.setUserid("-1");
				friendService.saveFriendGroup(fg);
			}
		}
	}

	public void repairMenu() {
		logger.info("初始化菜单...");
		menuAuthDao.executeHql("delete MenuAuth");
		menuDao.executeHql("update Menu t set t.menu = null");
		menuDao.executeHql("delete Menu");

		// 一
		Menu menu = new Menu();
		menu.setId(UUID.randomUUID().toString());
		menu.setName("系统管理");
		menu.setSeq(0);
		menuDao.save(menu);

		Menu rygl = new Menu();
		rygl.setId(UUID.randomUUID().toString());
		rygl.setName("数据管理员帐号管理");
		rygl.setSeq(1);
		rygl.setIconCls("icon-user");
		rygl.setUrl("app/user/manager/user_list.jsp");
		rygl.setMenu(menu);
		menuDao.save(rygl);

		Menu dlrz = new Menu();
		dlrz.setId(UUID.randomUUID().toString());
		dlrz.setName("登录日志");
		dlrz.setSeq(2);
		dlrz.setIconCls("icon-computer");
		dlrz.setUrl("app/user/log/login_log.jsp");
		dlrz.setMenu(menu);
		menuDao.save(dlrz);

		Menu czrz = new Menu();
		czrz.setId(UUID.randomUUID().toString());
		czrz.setName("操作日志");
		czrz.setSeq(3);
		czrz.setIconCls("icon-contacts");
		czrz.setUrl("app/user/log/operate_log.jsp");
		czrz.setMenu(menu);
		menuDao.save(czrz);

		// 二
		Menu qygl = new Menu();
		qygl.setId(UUID.randomUUID().toString());
		qygl.setName("数据管理员");
		qygl.setSeq(1);
		menuDao.save(qygl);

		Menu qyzhgl = new Menu();
		qyzhgl.setId(UUID.randomUUID().toString());
		qyzhgl.setName("企业账号管理");
		qyzhgl.setSeq(2);
		qyzhgl.setIconCls("icon-company");
		qyzhgl.setUrl("app/user/manager/company_list.jsp");
		qyzhgl.setMenu(qygl);
		menuDao.save(qyzhgl);

		Menu sjtb = new Menu();
		sjtb.setId(UUID.randomUUID().toString());
		sjtb.setName("上报任务管理");
		sjtb.setSeq(3);
		sjtb.setIconCls("icon-list");
		sjtb.setUrl("app/company/datareport/task/data_report_task_list.jsp");
		sjtb.setMenu(qygl);
		menuDao.save(sjtb);

		Menu bbgl = new Menu();
		bbgl.setId(UUID.randomUUID().toString());
		bbgl.setName("报表管理");
		bbgl.setSeq(4);
		bbgl.setIconCls("icon-list");
		bbgl.setUrl("");
		bbgl.setMenu(qygl);
		menuDao.save(bbgl);

		Menu bbsh = new Menu();
		bbsh.setId(UUID.randomUUID().toString());
		bbsh.setName("报表审核");
		bbsh.setSeq(1);
		bbsh.setIconCls("icon-list");
		bbsh.setUrl("app/company/datareport/manager/report_audit.jsp");
		bbsh.setMenu(bbgl);
		menuDao.save(bbsh);

		Menu rgcb = new Menu();
		rgcb.setId(UUID.randomUUID().toString());
		rgcb.setName("人工催报");
		rgcb.setSeq(2);
		rgcb.setIconCls("icon-list");
		rgcb.setUrl("");
		rgcb.setMenu(bbgl);
		menuDao.save(rgcb);

		Menu sjtj = new Menu();
		sjtj.setId(UUID.randomUUID().toString());
		sjtj.setName("数据统计");
		sjtj.setSeq(3);
		sjtj.setIconCls("icon-list");
		sjtj.setUrl("");
		sjtj.setMenu(bbgl);
		menuDao.save(sjtj);

		Menu xnfx = new Menu();
		xnfx.setId(UUID.randomUUID().toString());
		xnfx.setName("效能分析");
		xnfx.setSeq(4);
		xnfx.setIconCls("icon-list");
		xnfx.setUrl("");
		xnfx.setMenu(bbgl);
		menuDao.save(xnfx);

		Menu znbsjgl = new Menu();
		znbsjgl.setId(UUID.randomUUID().toString());
		znbsjgl.setName("智能表数据管理");
		znbsjgl.setSeq(5);
		znbsjgl.setIconCls("icon-list");
		znbsjgl.setUrl("");
		znbsjgl.setMenu(qygl);
		menuDao.save(znbsjgl);

		Menu dxgl = new Menu();
		dxgl.setId(UUID.randomUUID().toString());
		dxgl.setName("短信管理");
		dxgl.setSeq(6);
		dxgl.setIconCls("icon-sms");
		dxgl.setUrl("");
		dxgl.setMenu(qygl);
		menuDao.save(dxgl);

		Menu fzsjwh = new Menu();
		fzsjwh.setId(UUID.randomUUID().toString());
		fzsjwh.setName("辅助数据维护");
		fzsjwh.setSeq(7);
		fzsjwh.setIconCls("icon-list");
		fzsjwh.setUrl("");
		fzsjwh.setMenu(qygl);
		menuDao.save(fzsjwh);

		Menu zbxs = new Menu();
		zbxs.setId(UUID.randomUUID().toString());
		zbxs.setName("折标系数表维护");
		zbxs.setSeq(1);
		zbxs.setIconCls("icon-list");
		zbxs.setUrl("app/company/datareport/zhebiao_list.jsp");
		zbxs.setMenu(fzsjwh);
		menuDao.save(zbxs);

		Menu znbgl = new Menu();
		znbgl.setId(UUID.randomUUID().toString());
		znbgl.setName("智能表管理");
		znbgl.setSeq(2);
		znbgl.setIconCls("icon-list");
		znbgl.setUrl("");
		znbgl.setMenu(fzsjwh);
		menuDao.save(znbgl);

		Menu qyrz = new Menu();
		qyrz.setId(UUID.randomUUID().toString());
		qyrz.setName("企业日志管理");
		qyrz.setSeq(8);
		qyrz.setIconCls("icon-list");
		qyrz.setUrl("app/user/log/company_operate_log.jsp");
		qyrz.setMenu(qygl);
		menuDao.save(qyrz);

		// 三
		Menu qysjy = new Menu();
		qysjy.setId(UUID.randomUUID().toString());
		qysjy.setName("企业数据员");
		qysjy.setSeq(2);
		menuDao.save(qysjy);

		Menu qybbgl = new Menu();
		qybbgl.setId(UUID.randomUUID().toString());
		qybbgl.setName("企业报表管理");
		qybbgl.setSeq(1);
		qybbgl.setIconCls("icon-list");
		qybbgl.setUrl("");
		qybbgl.setMenu(qysjy);
		menuDao.save(qybbgl);

		Menu bbsjll = new Menu();
		bbsjll.setId(UUID.randomUUID().toString());
		bbsjll.setName("报表数据录入");
		bbsjll.setSeq(1);
		bbsjll.setIconCls("icon-list");
		bbsjll.setUrl("app/company/datareport/datawriter/data_write_list.jsp");
		bbsjll.setMenu(qybbgl);
		menuDao.save(bbsjll);

		Menu dshbb = new Menu();
		dshbb.setId(UUID.randomUUID().toString());
		dshbb.setName("待审核的报表");
		dshbb.setSeq(2);
		dshbb.setIconCls("icon-list");
		dshbb.setUrl("app/company/datareport/datawriter/data_wait_audit.jsp");
		dshbb.setMenu(qybbgl);
		menuDao.save(dshbb);

		Menu lsbb = new Menu();
		lsbb.setId(UUID.randomUUID().toString());
		lsbb.setName("历史报表查询");
		lsbb.setSeq(3);
		lsbb.setIconCls("icon-list");
		lsbb.setUrl("app/company/datareport/datawriter/data_has_audit.jsp");
		lsbb.setMenu(qybbgl);
		menuDao.save(lsbb);

		// 初始化角色
		sysRoleMemberDao.executeHql("delete SysRoleMembers");
		sysRoleDao.executeHql("delete SysRoles");

		SysRoles xtgly = new SysRoles();
		xtgly.setId(UUID.randomUUID().toString());
		xtgly.setRoleName("系统管理员");
		xtgly.setSeq(0);
		sysRoleDao.save(xtgly);

		SysRoles sjgly = new SysRoles();
		sjgly.setId(UUID.randomUUID().toString());
		sjgly.setRoleName("数据管理员");
		sjgly.setSeq(1);
		sysRoleDao.save(sjgly);

		SysRoles qylly = new SysRoles();
		qylly.setId(UUID.randomUUID().toString());
		qylly.setRoleName("企业数据员");
		qylly.setSeq(2);
		sysRoleDao.save(qylly);

		// 系统管理员菜单
		MenuAuth xtglyMenu = new MenuAuth();
		xtglyMenu.setId(UUID.randomUUID().toString());
		xtglyMenu.setMemberid(xtgly.getId());
		xtglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		xtglyMenu.setMenu(rygl);
		menuAuthDao.save(xtglyMenu);

		xtglyMenu = new MenuAuth();
		xtglyMenu.setId(UUID.randomUUID().toString());
		xtglyMenu.setMemberid(xtgly.getId());
		xtglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		xtglyMenu.setMenu(dlrz);
		menuAuthDao.save(xtglyMenu);

		xtglyMenu = new MenuAuth();
		xtglyMenu.setId(UUID.randomUUID().toString());
		xtglyMenu.setMemberid(xtgly.getId());
		xtglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		xtglyMenu.setMenu(czrz);
		menuAuthDao.save(xtglyMenu);

		// 数据管理员菜单
		MenuAuth sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(qyzhgl);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(sjtb);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(bbgl);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(bbsh);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(rgcb);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(sjtj);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(xnfx);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(znbsjgl);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(dxgl);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(fzsjwh);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(zbxs);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(znbgl);
		menuAuthDao.save(sjglyMenu);

		sjglyMenu = new MenuAuth();
		sjglyMenu.setId(UUID.randomUUID().toString());
		sjglyMenu.setMemberid(sjgly.getId());
		sjglyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		sjglyMenu.setMenu(qyrz);
		menuAuthDao.save(sjglyMenu);

		// 企业数据员菜单
		MenuAuth qisjyMenu = new MenuAuth();
		qisjyMenu.setId(UUID.randomUUID().toString());
		qisjyMenu.setMemberid(qylly.getId());
		qisjyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		qisjyMenu.setMenu(qysjy);
		menuAuthDao.save(qisjyMenu);

		qisjyMenu = new MenuAuth();
		qisjyMenu.setId(UUID.randomUUID().toString());
		qisjyMenu.setMemberid(qylly.getId());
		qisjyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		qisjyMenu.setMenu(bbsjll);
		menuAuthDao.save(qisjyMenu);

		qisjyMenu = new MenuAuth();
		qisjyMenu.setId(UUID.randomUUID().toString());
		qisjyMenu.setMemberid(qylly.getId());
		qisjyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		qisjyMenu.setMenu(dshbb);
		menuAuthDao.save(qisjyMenu);

		qisjyMenu = new MenuAuth();
		qisjyMenu.setId(UUID.randomUUID().toString());
		qisjyMenu.setMemberid(qylly.getId());
		qisjyMenu.setType(Constant.MENU_MEMBER_TYPE_ROLE);
		qisjyMenu.setMenu(lsbb);
		menuAuthDao.save(qisjyMenu);
	}

	public void repairAdmin() {
		String hql = "from Tuser where name='admin'";
		Tuser tuser = userDao.get(hql);
		if (null == tuser) {
			Puser user = new Puser();
			user.setId("0");
			user.setName("admin");
			user.setDisplayName("admin");
			user.setPwd("123456");
			user.setUserType(Constant.USERTYPE_USER);
			userService.save(user);
		}
	}
}
